package com.zxl.always.on.display.ui.widget.clock.digital;

import android.content.Context;
import android.graphics.Typeface;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.zxl.always.on.display.R;
import com.zxl.always.on.display.model.PaletteSet;
import com.zxl.always.on.display.tools.GraphicUtils;
import com.zxl.always.on.display.ui.widget.NonePaddingTextView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;


/**
 * Created by Administrator on 2018/2/28.
 */

public class DigitalClock3Widget extends BaseTimeViewHolder {

    private TextView mCalendarView, mWeekView;

    private NonePaddingTextView mHourView, mMinuteView, mAmAndPmView;

    public DigitalClock3Widget(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mHourView = findViewById(R.id.panel_clock_time);
        mMinuteView = findViewById(R.id.panel_clock_time_02);
        Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/clock2016.ttf");
        mHourView.setTypeface(typeface);
        mMinuteView.setTypeface(typeface);
        mAmAndPmView = findViewById(R.id.clock_ampm_right);
        mCalendarView = findViewById(R.id.panel_clock_calendar);
        mWeekView = findViewById(R.id.panel_clock_calendar2);
    }

    @Override
    public void refreshTime() {
        if (DateFormat.is24HourFormat(getContext())) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("HH", Locale.getDefault());
            mHourView.setText(dateFormat.format(System.currentTimeMillis()));
        } else {
            SimpleDateFormat dateFormat = new SimpleDateFormat("h", Locale.getDefault());
            mHourView.setText(dateFormat.format(System.currentTimeMillis()));

            SimpleDateFormat dateFormatA = new SimpleDateFormat("a", Locale.getDefault());
            mAmAndPmView.setVisibility(View.VISIBLE);
            mAmAndPmView.setText(dateFormatA.format(System.currentTimeMillis()));
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("mm", Locale.getDefault());
        mMinuteView.setText(dateFormat.format(System.currentTimeMillis()));
        mAmAndPmView.setVisibility(View.GONE);

        Date date = Calendar.getInstance().getTime();
        dateFormat = new SimpleDateFormat("MMMM d日, ", Locale.getDefault());
        mCalendarView.setText(dateFormat.format(date));
        dateFormat = new SimpleDateFormat("EEEE", Locale.getDefault());
        mWeekView.setText(dateFormat.format(date));
    }

    @Override
    public void setPaletteColor(int position) {
        GraphicUtils.setLinearGradientTextColor(mHourView, PaletteSet.getPaletteItems().get(position));
        GraphicUtils.setLinearGradientTextColor(mMinuteView, PaletteSet.getPaletteItems().get(position));
        GraphicUtils.setLinearGradientTextColor(mAmAndPmView, PaletteSet.getPaletteItems().get(position));
    }
}
